<?php
/**
 * 素材库审核
 */
namespace App;
use Model\MediaMod;
use Model\MediaPicMod;
use Model\MediaVideoMod;
use Pub\wx\WxTpl;
use Pub\tool\Setting as SettingCache;

defined('IN_IA') or exit('Access Denied');

class MediaCheck extends Action
{
    private $Media = null;
    private $MediaPic = null;
    private $MediaVideo = null;

    public function __construct()
    {
        parent::__construct();
        $this->Media = new MediaMod();
    }

    /**
     * 获取需要审核的总数
     */
    public function getTotal()
    {
        $_where = array("uniacid"=>$this->_uniacid,"status"=>2);
        $this->_result = array("status"=>"success","data"=>$this->Media->total($_where));
        $this->result($this->_result);
    }

    /**
     * 获取列表
     * @param array limit 请求下标
     */
    public function getList()
    {
        $_where = array("uniacid"=>$this->_uniacid,"status"=>2);
        $_list = $this->Media->findAll(
            $_where,
            array("order"=>array("id","desc"),"limit"=>$this->_gpc["limit"]),
            array("id","uid","type","time")
        );
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_member = $this->uidToInfo($_v["uid"]);
                $_list[$_k]["nickname"] = $_member["nickname"];
                $_list[$_k]["time"] = date("Y-m-d H:i:s",$_v["time"]);
                if($_v["type"] == "image"){
                    $_list[$_k]["media"] = $this->idToPic($_v["id"]);
                }else{
                    $_video = $this->idToVideo($_v["id"]);
                    $_list[$_k]["media"] = tomedia($_video["thumb"]);
                    $_list[$_k]["video"] = tomedia($_video["source"]);
                }
            }
            unset($_member);
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 通过素材id返回素材
     * @param integer $_media_id 素材id
     * @return string 图片封面的地址
     */
    private function idToPic($_media_id)
    {
        if(!$this->MediaPic instanceof MediaPicMod) $this->MediaPic = new MediaPicMod();
        $_pic = $this->MediaPic->findOne(array("media_id"=>$_media_id),array("thumb"));
        return tomedia($_pic["thumb"]);
    }

    /**
     * 通过id返回视频素材库的封面和视频链接
     * @param int $_media_id 素材库id
     * @return array 返回视频的封面和视频链接
     */
    private function idToVideo($_media_id)
    {
        if(!$this->MediaVideo instanceof MediaVideoMod) $this->MediaVideo = new MediaVideoMod();
        return $this->MediaVideo->findOne(array("media_id"=>$_media_id),array("thumb","source"));
    }

    /**
     * 通过审核
     * @param integer media_id 素材id
     */
    public function passOne()
    {
        if($this->pass($this->_gpc["media_id"])){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    /**
     * 删除素材
     * @param integer media_id 素材id
     * @param string type 素材类型  image || video
     */
    public function delOne()
    {
        if($this->del($this->_gpc["media_id"],$this->_gpc["type"])){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    /**
     * 批量操作
     * @param string type 操作类型  del=删除 pass=通过
     * @param array data 操作id数组
     */
    public function batchHandle()
    {
        $_data = $this->_gpc["data"];
        if($this->_gpc["type"] == "pass"){
            foreach ($_data as $_v){
                $this->pass($_v["media_id"]);
            }
        }else{
            $this->MediaPic = new MediaPicMod();
            foreach ($_data as $_v){
                $this->del($_v["media_id"],$_v["media_type"]);
            }
        }
        $this->_result = array("status"=>"success");
        $this->result($this->_result);
    }

    /**
     * 批量通过
     * @param string $_media_id 素材id
     * @return bool
     */
    private function pass($_media_id)
    {
        $this->sendTplMessage($_media_id,"pass");
        return $this->Media->update(array("status"=>1),array("id"=>$_media_id));
    }

    /**
     * 批量删除
     * @param string $_media_id 素材id
     * @param string $_type 素材类型 image = 图片 video=视频
     * @return bool
     */
    private function del($_media_id,$_type = 'image')
    {
        $this->sendTplMessage($_media_id,"del");
        $MediaType = null;
        if($_type == 'image'){
            $MediaType = new MediaPicMod();
            $_media = $MediaType->findOne(array('media_id'=>$_media_id),array('source','thumb'));
        }else{
            $MediaType = new MediaVideoMod();
            $_media = $MediaType->findOne(array('media_id'=>$_media_id),array('source','thumb'));
        }
        if($this->Media->delete(array('id'=>$_media_id))){
            if($MediaType->delete(array('media_id'=>$_media_id))){
                load()->func('file');
                file_delete($_media['source']);
                file_delete($_media['thumb']);
                file_remote_delete($_media['source']);
                file_remote_delete($_media['thumb']);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 发送模板消息
     * @param int $_media_id 素材id
     * @param $_type
     */
    private function sendTplMessage($_media_id,$_type = "pass")
    {
        $_media = $this->Media->findOne(array("id"=>$_media_id),array("uid"));
        $_member = $this->uidToInfo($_media["uid"]);
        $_fans = $this->uidToFans($_media["uid"]);
        $_openid = KUYUAN_DEBUG ? 'oxYQT1DAyMPEFXMzjjJ-Rz1OQa-c' : $_fans['openid'];
        $_data = array(
            "first"=>array(
                "value"=>'【'.SettingCache::get(SettingCache::WEB_NAME).'】素材审核结果'
            ),
            "keyword1"=>array(
                "value"=>$_member["nickname"]
            ),
            "keyword2"=>array(
                "value"=>$_type == "pass" ? "审核通过" : "审核失败，请重新上传素材"
            ),
            "keyword3"=>array(
                "value"=>date("Y-m-d H:i:s",TIMESTAMP)
            ),
            "remark"=>array(
                "value"=>$_type == "pass" ? "点击前往发红包" : "点击前往重新上传素材"
            )
        );
        $WxTpl = new WxTpl();
        $WxTpl->send(
            $_openid,
            SettingCache::get(SettingCache::MESSAGE_MEDIA_PASS),
            $_data,
            murl('entry//index',array('m'=>'kuyuan_red'),true,true)
        );
    }
}